home *** CD-ROM | disk | FTP | other *** search
- <?php
- /* album_functions.php
- * .net magazine (www.netmag.co.uk), issue 83
- * Matt Kynaston, 2001
- * Distributed under the GNU Public License - www.gnu.org/copyleft/gpl.html
- *
- * This file contains the PHP functions used in album.php
- * I've seperated it out to make editing the album.php simpler, and
- * so you can reuse these functions in your own scripts by simply
- * including this file along with the constant declarations in album.php
- */
-
- /* function uploadFile()
- * checks to see if file data has been posted, if so
- * copies it to photo dir with unique file name and creates thumbnail, then
- * prints success or error message
- */
- function uploadFile() {
- // import form data into local function
- global $ulFile, $ulFile_name, $ulFile_type;
-
- // check for form data
- if (isset($ulFile) && $ulFile_name) {
- // check uploaded file is one of supported types
- if ($ulFile_type == "image/png" ||
- $ulFile_type == "image/jpeg" ||
- $ulFile_type == "image/pjpeg") {
-
- $fn = getUniqueName($ulFile_name); // generate a unique file name
-
- /* move file to the photo directory. Notice we now use move_uploaded_file
- * instead of copy. It includes a check to make sure the file came from
- * the right source, making it more secure
- */
- if (move_uploaded_file($ulFile, PHOT_DIR."/$fn")) {
- print "<p>$ulFile_name successfully uploaded</p>\n";
- saveThumbnail($fn); // generate tumbnail image
- } else {
- print "<p>Oops! Couldn't upload $ulFile_name</p>\n";
- unlink($ulFile); // remove temporary file
- }
- } else {
- print "<p>Sorry, file type $ulFile_type not supported. Try a JPEG or PNG image.</p>\n";
- unlink($ulFile); // remove temporary file
- }
- }
- }
-
- /* function getUniqueName(original_filename)
- * Returns unique file name. Checks to see if the original file name exists. If so
- * searches for next available name by appending counter. Notice the 'ereg' function
- * used to split the filename into name and suffix - this uses a regular expression.
- * Also note that to join strings in PHP we use '.' instead of JavaScript's "+".
- */
- function getUniqueName($fn) {
- $counter=1; // counter to generate unique file name
- ereg("(.+)\.(.+)", $fn, $split_fn); // split file name into name and suffix
-
- // keep looping until a unique name is found
- while (file_exists(PHOT_DIR."/$fn")) {
- $fn = $split_fn[1]."_".$counter.".".$split_fn[2];
- $counter++;
- }
-
- return $fn;
- }
-
- /* function saveThumbnail(filename)
- * generates a thumnail image and saves it to the thumbnail directory
- * uses the GD extension to manipulate the images - this can't handle
- * GIF images for licensing reasons: the function uses JPEGs and PNGs.
- * If there is an error, or the file type is wrong, it generates a
- * 'no thumbnail available' image instead
- */
- function saveThumbnail($fn) {
- $fpath = PHOT_DIR."/$fn";
-
- /* GetImageSize returns an array:
- * $info[0] - horizontal size of image in pixels
- * $info[1] - vertical size of image in pixels
- * $info[2] - type of image (1=GIF, 2=JPEG, 3=PNG)
- */
- $info = GetImageSize("$fpath");
-
- // do we need to resize image?
- if ($info[0] > MAX_XY || $info[1] > MAX_XY) {
- // is image landscape?
- if ($info[0] >= $info[1]) {
- $width = MAX_XY;
- $height = $info[1]*MAX_XY/$info[0];
- // or portrait?
- } else {
- $height = MAX_XY;
- $width = $info[0]*MAX_XY/$info[1];
- }
- } else {
- // use original dimensions
- $width = $info[0];
- $height = $info[1];
- }
-
- // create new thumbnail image
- $im = ImageCreate($width, $height);
-
- /* determine image type and load original image. Notice new tests for image
- * types. The GD extension has changed a lot over the years, dropping GIFs
- * and adding PNG support. By testing, we avoid trying to process an image
- * PHP can't deal with
- */
- $im_big = ""; // default is no image
- switch ($info[2]) {
- case 1 :
- if (ImageTypes() & IMG_GIF) // test for GIF support
- $im_big = ImageCreateFromGIF("$fpath");
- break;
- case 2 :
- if (ImageTypes() & IMG_JPG) // test for JPEG support
- $im_big = ImageCreateFromJPEG("$fpath");
- break;
- case 3 :
- if (ImageTypes() & IMG_PNG) // test for PNG support
- $im_big = ImageCreateFromPNG("$fpath");
- break;
- }
-
- if ($im_big) {
- /* resize original image into thumbnail - see PHP Manual for details on
- * the arguements ImageCopyResized takes
- */
- ImageCopyResized($im,$im_big,0,0,0,0,$width,$height,$info[0],$info[1]);
- } else {
- // couldn't load original image, so generate 'no thumbnail' image instead
- $width = 100;
- $height = 100;
- $im = ImageCreate($width, $height); // create a blank image
- $bgc = ImageColorAllocate($im, 0, 0, 0); // background color = black
- $tc = ImageColorAllocate($im, 255, 255, 255); // text color = white
- ImageFilledRectangle($im, 0, 0, $width, $height, $bgc); // draw rectangle
- ImageString($im, 3, 9, 36, "No thumbnail", $tc); // add text
- ImageString($im, 3, 17, 48, "available", $tc);
- }
-
- /* save our image in thumb directory - if the second argument is left out,
- * the image gets sent to the browser, as in thumbnail.php
- */
- ImageJPEG($im, THUMB_DIR."/".$fn);
-
- // clean up our working images
- ImageDestroy($im_big);
- ImageDestroy($im);
- }
-
- /* function makeAlbumTable()
- * writes table filled with thumbnail images linked to full size images
- */
- function makeAlbumTable() {
- $col = 0; // column counter
- $dh = opendir(PHOT_DIR); // open photo directory
-
- // start table
- print "<table width='100%' border='0' cellspacing='10'>\n<tr align='center'>\n";
-
- // loop through directory
- while($file=readdir($dh)) {
- // skip directory files
- if ($file=="." || $file=="..") continue;
-
- // get size for use in image tag
- $info = GetImageSize(THUMB_DIR."/$file");
-
- // write table w/thumbnail linked to full sized image
- print "<td valign=middle><a href='".PHOT_DIR."/$file' target='_blank' onfocus='this.blur()'><img src='".THUMB_DIR."/$file' border=0 width=$info[0] height=$info[1]>";
- print "<br>$file</a></td>\n";
- $col++;
-
- // if max number of columns reached, end table row
- if ($col==NUM_COLS) {
- print "</tr>\n<tr align=center>\n";
- $col=0;
- }
- }
-
- // fill remaining cols with empty cells
- for ($i=$col;$i<NUM_COLS;$i++) {
- print "<td></td>\n";
- }
-
- // end table
- print "</tr></table>";
- }
-
- ?>